/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor;

import java.util.ArrayList;
import java.util.List;

import com.daikit.daikit4gxt.client.editor.DkSimpleBeanEditorDriver;
import com.daikit.daikit4gxt.client.log.DkLogger;
import com.daikit.daikit4gxt.client.ui.popup.DkLocalEditCreatePopup;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.editor.ShopDataEditor;
import com.daikit.daikit4gxtsample.client.propertyaccess.SupplierDetailPropertyAccess;
import com.daikit.daikit4gxtsample.shared.bean.ShopData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.google.gwt.core.client.GWT;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;


/**
 * Edit/Create popup for Shop
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class ShopEditCreatePopup extends DkLocalEditCreatePopup<ShopData, ShopDataEditor>
{

	private static ShopEditCreatePopup instance = null;

	/**
	 * @return the unique instance of this popup
	 */
	public static final ShopEditCreatePopup get()
	{
		//		if (instance == null)
		//		{
		instance = new ShopEditCreatePopup();
		//		}
		return instance;
	}

	protected ShopEditCreatePopup()
	{
		super(new ShopDataEditor());
		setWidth(800);
		addField(new FieldLabel(getEditor().id, Main.gwtSampleI18n().shop_id()));
		addField(new FieldLabel(getEditor().name, Main.gwtSampleI18n().shop_name()));
		addField(new FieldLabel(getEditor().city, Main.gwtSampleI18n().shop_city()));

		final List<ColumnConfig<SupplierDetailData, ?>> columnConfigs = new ArrayList<ColumnConfig<SupplierDetailData, ?>>();
		final ColumnConfig<SupplierDetailData, String> columnSupplierDetailId = new ColumnConfig<SupplierDetailData, String>(
				SupplierDetailPropertyAccess.INSTANCE.id(), 150, Main.gwtSampleI18n().supplier_detail_id());
		final ColumnConfig<SupplierDetailData, String> columnSupplierDetailName = new ColumnConfig<SupplierDetailData, String>(
				SupplierDetailPropertyAccess.INSTANCE.name(), 150, Main.gwtSampleI18n().supplier_detail_name());
		final ColumnConfig<SupplierDetailData, SupplierData> columnSupplierDetailSupplier = new ColumnConfig<SupplierDetailData, SupplierData>(
				SupplierDetailPropertyAccess.INSTANCE.supplier(), 150, Main.gwtSampleI18n().supplier_detail_supplier());

		columnConfigs.add(columnSupplierDetailId);
		columnConfigs.add(columnSupplierDetailName);
		columnConfigs.add(columnSupplierDetailSupplier);
		final ColumnModel<SupplierDetailData> columnModel = new ColumnModel<SupplierDetailData>(columnConfigs);
		final Grid<SupplierDetailData> grid = new Grid<SupplierDetailData>(new ListStore<SupplierDetailData>(
				SupplierDetailPropertyAccess.INSTANCE.key()), columnModel);

		grid.setBorders(true);
		grid.setHeight(150);

		for (int i = 0; i < 20; i++)
		{
			final SupplierDetailData data = new SupplierDetailData();
			data.setName("name=" + i);
			grid.getStore().add(data);
		}

		addField(grid);

		getEditor().id.setEnabled(false);
		setShowAfterSaveConfirmation(false);
	}

	interface ThisEditorDriver extends DkSimpleBeanEditorDriver<ShopData, ShopDataEditor>
	{
	}

	private final ThisEditorDriver driver = GWT.create(ThisEditorDriver.class);

	@Override
	protected ShopData createBeanInstance()
	{
		return new ShopData();
	}

	@Override
	protected void onModelSaved(final ShopData updatedModel)
	{
		DkLogger.debug(getClass(), "Shop saved locally");
	}

	@Override
	protected DkSimpleBeanEditorDriver<ShopData, ShopDataEditor> getDriver()
	{
		return driver;
	}

	@Override
	protected String getLabelPopupTitle()
	{
		return "Edit Shop";
	}

	@Override
	protected String getLabelActionSaveLoadingLabel()
	{
		return "Saving Shop...";
	}

	@Override
	public void invalidateUi()
	{
		// Nothing to be done here
	}

	@Override
	protected String getLabelPopupSaveOkMessageBoxMessage()
	{
		return null; // Not used
	}

	@Override
	protected String getLabelPopupSaveOkMessageBoxTitle()
	{
		return null; // Not used
	}

}
